﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Diagnostics;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Phone.Net.NetworkInformation;

namespace ee_wp_tourmaker.WebServices
{
    public class HighScoreService
    {
        public void startDownloading()
        {
            bool isAvailable = NetworkInterface.GetIsNetworkAvailable();
            if (isAvailable == false)
            {
                Debug.WriteLine("No internet connection for downloading highscores");
            }
            else
            {
                var client = new ee_wp_tourmaker.AzureService.Service1Client();
                client.OpenCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_OpenCompleted);
                client.OpenAsync(client);
                client.GetPersonScoreCompleted += client_GetPersonScoreCompleted;
            }
        }

        void client_OpenCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            var client = (ee_wp_tourmaker.AzureService.Service1Client)e.UserState;

            client.GetChallengeListAsync(null, client);
        }

        void client_GetPersonScoreCompleted(object sender, AzureService.GetPersonScoreCompletedEventArgs e)
        {
            var array = new List<DataObjectsPhone.PlayerScore>();

            foreach (AzureService.PersonScore score in e.Result)
            {
                App.PlayerScores.Add(new DataObjectsPhone.PlayerScore
                {
                    Name = score.Name,
                    Id = score.Id,
                    Points = score.Points
                });
            }
        }
    }
    
}
